import {Params, useLocation, useParams as useRouteParams,} from 'react-router-dom'
import {useContext} from 'react'
import {WorkspaceRouteParamsContext} from '../components/WorkspaceTabs/WorkspaceContent.tsx'

export function useIsWorkspace() {
  const { pathname } = useLocation()
  return pathname === '/homepage/index'
}

export function useParams<
  ParamsOrKey extends string | Record<string, string | undefined> = string,
>(): Readonly<
  [ParamsOrKey] extends [string] ? Params<ParamsOrKey> : Partial<ParamsOrKey>
> {
  const params = useRouteParams()
  const workspaceParams = useContext(WorkspaceRouteParamsContext)
  const isWorkspace = useIsWorkspace()
  return (isWorkspace ? workspaceParams : params) as any
}
